package com.secneo.appshare.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.secneo.appshare.R;
import com.secneo.appshare.activities.AppsOtherUserActivity;
import com.secneo.appshare.api.LocalStore;
import com.secneo.appshare.api.RemoteApi.Comment;

public class CommentsAdapter extends BaseAdapter{

	private List<Comment> mList;
	private Context ctx;
	
	public CommentsAdapter(Context ctx, List<Comment> list) {
		this.ctx = ctx;
		this.mList = list;
	}
	
	public void freeDrawable()
	{
		for (int i = 0; i < mList.size(); i++)
		{
			if (!mList.get(i).user_icon.contains("buildin") &&
					mList.get(i).user_icon_drawable != null)
			{
				BitmapDrawable a = (BitmapDrawable) mList.get(i).user_icon_drawable;
				if (a != null && !a.getBitmap().isRecycled())
					a.getBitmap().recycle();
				mList.get(i).user_icon_drawable = null;
			}
		}
		//mList.clear();
		System.gc();
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		final Comment comment = mList.get(position);
		CommentItem item;
		if (convertView == null) {
			View v = LayoutInflater.from(ctx).inflate(R.layout.nui_include_comment_feed_item, null);
			item = new CommentItem();
			item.userIcon = (ImageView) v.findViewById(R.id.comment_user_icon);
			item.userName = (TextView) v.findViewById(R.id.comment_user_name);
			item.text_comment = (TextView)v.findViewById(R.id.text_comment);
			v.setTag(item);
			convertView = v;
		} 
		else 
		{
			item = (CommentItem)convertView.getTag();
		}
		
		if (comment.user_icon_drawable == null)
			item.userIcon.setImageResource(R.drawable.some_user);
		else 
			item.userIcon.setImageDrawable(comment.user_icon_drawable);
		item.userName.setText(comment.uname);
		item.text_comment.setText(comment.comment);
		
		item.userIcon.setOnClickListener(new OnClickListener()
		{		
			@Override
			public void onClick(View v)
			{
				try 
				{
					Intent intent = new Intent();
					intent.setClass(ctx, AppsOtherUserActivity.class);

					Bundle mBundle = new Bundle();
					String uid = LocalStore.getLocaleStoreInstance(ctx).getUid();
					mBundle.putString("uid", uid);
					mBundle.putString("other_uid", comment.uid);
					mBundle.putString("user_icon", comment.user_icon);
					mBundle.putString("user_name", comment.uname);
					intent.putExtras(mBundle);
					ctx.startActivity(intent);
					
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		});
		
		return convertView;
	}
	class CommentItem 
	{
		ImageView userIcon;
		TextView userName;
		TextView text_comment;
	}
}
